<?php
include('../../includes/classes/core.php');

$overhead_detail_id = 163;

$real_quantity 		= 120;
$real_cost			= 2;

$res = mysql_fetch_assoc(mysql_query("SELECT		self_cost_history.id,
													self_cost_history.date,
													self_cost_history.production_id,
													self_cost_history.current_quantity,
													self_cost_history.current_cost,
													self_cost_history.income_quantity,
													self_cost_history.income_cost
										FROM    	self_cost_history
										WHERE   	self_cost_history.overhead_detail_id = $overhead_detail_id"));

$id					= $res[id];
$production_id		= $res[production_id];
$date				= $res[date];

$current_quantity   = $res[current_quantity];
$current_cost		= $res[current_cost];
$income_quantity    = $res[income_quantity];

$result_quantity	= $current_quantity + $real_quantity;
$result_cost		= ($current_quantity * $current_cost + $real_quantity * $real_cost) / ($current_quantity + $real_quantity);

$diff              	= $real_quantity - $income_quantity;

mysql_query("UPDATE self_cost_history
			 SET	self_cost_history.income_quantity 	= $real_quantity,
					self_cost_history.income_cost 		= $real_cost,
					self_cost_history.result_quantity 	= $result_quantity,
					self_cost_history.result_cost 		= $result_cost
			WHERE   self_cost_history.id = $id");

$res1 = mysql_query("SELECT		self_cost_history.id,
								self_cost_history.current_quantity,
								self_cost_history.current_cost,
								self_cost_history.income_quantity,
								self_cost_history.income_cost
					FROM    	self_cost_history
					WHERE   	self_cost_history.id > $id AND self_cost_history.production_id = $production_id
					ORDER BY 	self_cost_history.id ASC");

while ($row = mysql_fetch_assoc($res1)) {
	
	$current_quantity 	= $row[current_quantity] + $diff;
	$current_cost		= $result_cost;
	
	$result_quantity	= $current_quantity + $row[income_quantity];
	$result_cost 		= ($current_quantity * $current_cost +  $row[income_quantity] * $row[income_cost]) / ($current_quantity + $row[income_quantity]);
	
	
	mysql_query("UPDATE self_cost_history
				 SET	self_cost_history.current_quantity 	= $current_quantity,
						self_cost_history.current_cost 		= $current_cost,
						self_cost_history.result_quantity 	= $result_quantity,
						self_cost_history.result_cost 		= $result_cost
				WHERE   self_cost_history.id 				= $row[id]");
	
	echo $row[id].'/'.$current_quantity.'-'.$row[current_quantity].'</br>';

}

$res2 = mysql_query("	SELECT 		sale_transaction.end_date,
									sale_transaction_detail.id AS `det_id`
						FROM   		sale_transaction
						JOIN    	sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id
						WHERE 		sale_transaction.end_date > '$date' AND sale_transaction_detail.production_id = $production_id
						ORDER BY 	sale_transaction.end_date ASC");

$res3 = mysql_query("	SELECT 		`self1`.`date` AS `start`,
									(SELECT `self_cost_history`.`date` FROM  `self_cost_history` WHERE `self_cost_history`.`date` > `self1`.`date` AND `self_cost_history`.`production_id` = $production_id LIMIT 1) AS `end`,
									`self1`.`result_cost` AS `self_cost`
						FROM 		`self_cost_history` AS `self1`
						WHERE    	`self1`.`production_id` = 202
						ORDER BY 	`self1`.`date` ASC");

while ($row2 = mysql_fetch_assoc($res2)) {
	while ($row3 = mysql_fetch_assoc($res3)) {
		if ($row2[end_date] > $row3[start] && $row2[end_date] < $row3[end]) {
			echo 'yes';
			mysql_query("UPDATE `sale_transaction_detail`
						 SET     sale_transaction_detail.self_cost = $row3[self_cost]
						 WHERE   sale_transaction_detail.production_id = $production_id AND sale_transaction_detail.id >= $row2[det_id]");
		}
	}
}






























